package com.senjoeson.teamfriends.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.senjoeson.teamfriends.R;
import com.senjoeson.teamfriends.bean.User;
import com.senjoeson.teamfriends.utils.FormatUtils;
import com.senjoeson.teamfriends.utils.MyToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.email)
    AppCompatAutoCompleteTextView mEmail;
    @BindView(R.id.userpwd)
    AppCompatAutoCompleteTextView mUserpwd;
    @BindView(R.id.confirmpwd)
    AppCompatAutoCompleteTextView mConfirmpwd;
    @BindView(R.id.reister)
    Button mRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        mRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String emails = mEmail.getText().toString().trim();
        String userpass = mUserpwd.getText().toString().trim();
        String confirmPwd = mConfirmpwd.getText().toString().trim();
        if (TextUtils.isEmpty(emails) || TextUtils.isEmpty(userpass) ||
                TextUtils.isEmpty(confirmPwd)) {
            Toast.makeText(this, "邮箱或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else if (!FormatUtils.checkEmail(emails)) {
            MyToast.showToast("邮箱格式不正确");
            return;
        } else if (!TextUtils.equals(userpass, confirmPwd)) {
            Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            return;
        }
//注册逻辑
        BmobUser user = new BmobUser();
        user.setUsername(emails);
        user.setEmail(emails);
        user.setPassword(userpass);
        user.signUp(new SaveListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null) {
                    MyToast.showToast("注册成功" + user.toString());
                    startActivity(new Intent(RegisterActivity.this, UserInfoActivity.class));
                    finish();
                } else {
                    MyToast.showToast(e.toString());
                }
            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
